package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.LoaiDanhMucDao;
import com.tt.fashionWebSite.dao.LoaiDanhMucSanPhamDao;
import com.tt.fashionWebSite.domain.KichThuoc;
import com.tt.fashionWebSite.domain.LoaiDanhMuc;
import com.tt.fashionWebSite.domain.LoaiDanhMucSanPham;
import com.tt.fashionWebSite.dto.JsonResponseCode;
import com.tt.fashionWebSite.dto.KichThuocDto;
import com.tt.fashionWebSite.dto.LoaiDanhMucDto;
import com.tt.fashionWebSite.dto.LoaiDanhMucSanPhamDto;
import com.tt.fashionWebSite.service.Ad_ProductListProductService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("ad_productListProductService")
public class Ad_ProductListProductServiceImp extends GenericService implements Ad_ProductListProductService {
    @Autowired
    @Qualifier("loaiDanhMucSanPham")
    private LoaiDanhMucSanPhamDao loaiDanhMucSanPhamDao;

    @Autowired
    @Qualifier("loaiDanhMuc")
    private LoaiDanhMucDao loaiDanhMucDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminProductListProduct(LoaiDanhMucSanPhamDto productListProductDto) {
        LoaiDanhMucSanPham loaiDanhMucSanPham = loaiDanhMucSanPhamDao.findById(productListProductDto.getMaLoaiDMSP());
        if(loaiDanhMucSanPham == null){
            loaiDanhMucSanPham = new LoaiDanhMucSanPham();
            loaiDanhMucSanPham.setMaLoaiDMSP(productListProductDto.getMaLoaiDMSP());
            loaiDanhMucSanPham.setTenLoaiDMSP(productListProductDto.getTenLoaiDMSP());
            LoaiDanhMuc loaiDanhMuc = loaiDanhMucDao.findById(productListProductDto.getLoaiDanhMuc().getMaLoai());
            loaiDanhMucSanPham.setLoaiDanhMuc(loaiDanhMuc);
            loaiDanhMucSanPhamDao.insert(loaiDanhMucSanPham);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    public List<LoaiDanhMucSanPhamDto> getAllProductList () {
        return mapper.mapCollection(loaiDanhMucSanPhamDao.getAllProductList(), LoaiDanhMucSanPhamDto.class);
    }

    public List<LoaiDanhMucDto> getAllProductCategory () {
        return mapper.mapCollection(loaiDanhMucDao.getAllProductCategory(), LoaiDanhMucDto.class);
    }

    public LoaiDanhMucSanPhamDto getListProductInfoEdit(LoaiDanhMucSanPhamDto productListDto){
        LoaiDanhMucSanPham loaiDanhMucSanPham = loaiDanhMucSanPhamDao.findById(productListDto.getMaLoaiDMSP());
        LoaiDanhMucSanPhamDto loaiDanhMucSanPhamDto = mapper.map(loaiDanhMucSanPham, LoaiDanhMucSanPhamDto.class,LOAI_DANH_MUC_SAN_PHAM_MAP_ID);
        return  loaiDanhMucSanPhamDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public LoaiDanhMucSanPhamDto registerProductListEdit(LoaiDanhMucSanPhamDto productListDto) {
        LoaiDanhMucSanPham loaiDanhMucSanPham = loaiDanhMucSanPhamDao.findById(productListDto.getMaLoaiDMSP());
        try {
            loaiDanhMucSanPham.setMaLoaiDMSP(productListDto.getMaLoaiDMSP());
            loaiDanhMucSanPham.setTenLoaiDMSP(productListDto.getTenLoaiDMSP());
            LoaiDanhMuc loaiDanhMuc = loaiDanhMucDao.findById(productListDto.getLoaiDanhMuc().getMaLoai());
            loaiDanhMucSanPham.setLoaiDanhMuc(loaiDanhMuc);
            loaiDanhMucSanPhamDao.update(loaiDanhMucSanPham);
            return productListDto;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminProductListProductDelete(List<LoaiDanhMucSanPhamDto> productListProductDto) {
        for(int i = 0; i < productListProductDto.size(); i++){
            loaiDanhMucSanPhamDao.delete(productListProductDto.get(i).getMaLoaiDMSP());
        }
        return JsonResponseCode.SUCCESS;
    }
}
